package com.epro.dx.activity;

import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.epro.dx.R;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.weipu.common.DXApplication;
import com.weipu.common.DXApplication.TrackerName;
import com.weipu.common.constants.Constant;
import com.weipu.common.util.Logger;

/**
 *  settings about us contact us help
 * 
 * @author  hjd008
 * @version  [版本号, 2015-1-14]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class InfoChildActivity extends BaseFragmentActivity
{
    private static final String TAG="InfoChildActivity";
    private WebView webView;
    
    /**
     * 1：about us 2：contact us 3：help
     */
    private int settingsItem;
	private Tracker tracker;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        tracker = DXApplication.getContext().getTracker(TrackerName.APP_TRACKER);
        //设置窗体无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_infochild);
        setLeftBtnContent(true, -1);
        Bundle data = this.getIntent().getExtras();
        if (null != data)
        {
            settingsItem = data.getInt("settings_item", 1);
        }
        //setLeftBtnToHome(HomeActivity.titleArray[3]);
        initwebview();
    }
    
    private void initwebview()
    {
        webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setJavaScriptEnabled(true);
        //webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

        //webView.setInitialScale(98);
        webView.setWebViewClient(new WebViewClient()
        {
            //跳过HTTPS验证
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error)
            {
//                handler.proceed();
            }
            
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
            	//自适应屏幕
            	view.getSettings().setUseWideViewPort(true); 
            	view.getSettings().setLoadWithOverviewMode(true);
            	view.loadUrl(url); 
            	showWaitingDialog();
            	Logger.i(TAG, "current Url: "+url);
            	return super.shouldOverrideUrlLoading(view, url);
            }
            
        });
        webView.setWebChromeClient(new WebChromeClient()
        {
            @Override
            public void onProgressChanged(WebView view, int newProgress)
            {
            	Logger.i(TAG, "newProgress: "+newProgress);
                if (newProgress > 80)
                {
                	Logger.i(TAG, "newProgress > 80 hideWaitingDialog");
                    hideWaitingDialog();
                }
            }
        });
        //根据moreactivity里的列表选择。让webview来加载相应的说明文字页面
        String screenName = null;
        switch (settingsItem)
        {
        //            case 1:
        //                setTitleContent(R.string.settings_aboutus);
        //                webView.loadUrl("file:///android_asset/about_as.htm");
        //                setContentView(R.layout.activity_infochild);
        //                break;
            case 2:
                setTitleContent(R.string.settings_contactus);
                screenName = getString(R.string.settings_contactus);
                //webView.loadUrl("file:///android_asset/contact_us.htm");
                webView.loadUrl("file:///android_asset/Contact_Us-Android-20150626.htm");
                break;
            case 3:
                setTitleContent(R.string.settings_help);
                screenName = getString(R.string.settings_help);
//                webView.loadUrl("file:///android_asset/Application.html");
                webView.loadUrl("file:///android_asset/Help-Android-20150626.htm");
//                webView.loadUrl("http://www.dx.com");
                
                break;
            case 4:
                setTitleContent(R.string.settings_privacy);
                screenName = getString(R.string.settings_privacy);
//                webView.loadUrl("file:///android_asset/Application.html");
                webView.loadUrl("file:///android_asset/Privacy_Statement.htm");
//                webView.loadUrl("http://www.dx.com");
                
                break;
            case 5:
                setTitleContent(R.string.settings_condition);
                screenName = getString(R.string.settings_condition);
//                webView.loadUrl("file:///android_asset/Application.html");
//                webView.loadUrl("file:///android_asset/Conditions.htm");
                webView.loadUrl(getResources().getString(R.string.condition_url));
                
                break;

            case 6:

                setTitleContent(R.string.settings_google_play);
                screenName = getString(R.string.settings_google_play);
//                webView.loadUrl("file:///android_asset/Application.html");
//                webView.loadUrl("file:///android_asset/Conditions.htm");
//                webView.loadUrl(getResources().getString(R.string.condition_url));
                webView.loadUrl(Constant.APP_UPDATE_URL);

            default:
                break;
        }
        tracker.setScreenName(screenName);
        tracker.send(new HitBuilders.ScreenViewBuilder().build());
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
        	hideWaitingDialog();
            if (null != webView && webView.canGoBack())
            {
                webView.stopLoading();
                webView.goBack();
            }
            else
            {
                this.finish();
            }
        }
        return true;
    }
}
